﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;

public class LoadABFromDisk : MonoBehaviour {

    AssetBundle abs = null;
    // Use this for initialization
    IEnumerator Start () {
        string myPath = "Assets/AssetBundles/prefabs/models";
        //第一种从本地加载
        //AssetBundle ab = AssetBundle.LoadFromFile(myPath);
        //AssetBundle assetBundle;
        //assetBundle.LoadAsset()

        //加载所有
        //object[] abs = ab.LoadAllAssets();
        //foreach (GameObject item in abs)
        //{
        //    Instantiate(item);
        //}

        //第二种 从内存加载
        //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(myPath));

        //第三种 网络请求，舍弃了WWW的方式
        string url = @"file:///E:\Learnings\Unity 132 AssetBundle从入门到掌握(基于Unity2017)\源码工程、PPT\webserver\AssetBundles\models";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.SendWebRequest();

        abs = DownloadHandlerAssetBundle.GetContent(request);

        //只实例化一个物体
        GameObject go = abs.LoadAsset<GameObject>("Cube");
        //GameObject go = ab.LoadAsset<GameObject>("Cube");
        Instantiate(go);

	}

    IEnumerator GetABbyNet()
    {
        string url = @"file:///E:\Learnings\Unity 132 AssetBundle从入门到掌握(基于Unity2017)\源码工程、PPT\webserver\AssetBundles\models";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.SendWebRequest();

        abs = DownloadHandlerAssetBundle.GetContent(request);
    }
	
}
